///////////////////////////////////////////////////////////
//  CSaveView.cpp
//  Implementation of the Class CSaveView
//  Created on:      12-Oct-2013 11:36:32 AM
//  Original author: Jakkaphan Aiemcharoenjit
///////////////////////////////////////////////////////////

#include "CSaveView.h"
#include "ControlCommand.h"
#include "CurrentCommand.h"
#include "stdafx.h"

using namespace UI;
using namespace UI::CMD;

CSaveView::CSaveView(IWindowDirector* director)
{
  this->m_type = IViewCtrl::SAVEVIEW_T;
  this->m_parrentWnd = director;

  // call initialize for create all UI.
  Initialize();
}

CSaveView::~CSaveView()
{

}

UI::IViewCtrl::ViewType CSaveView::GetType() const 
{

	return  this->m_type;
}


void CSaveView::Initialize()
{
  this->SetAttribute("x1", 0);
  this->SetAttribute("y1", 0);
  this->SetAttribute("width", "100%");
  this->SetAttribute("height", "100%");
  this->SetAttribute("backgroundColour", CColour(COLOUR_PAGE));
  this->SetEventHandler("clickview", this, &CSaveView::OnClickedView);

  // Create navigationbar and set right button name
  this->m_navigationBar = CreateNavigationBar(CAttributes()
    .Set("name", "navBar")
    .Set("title", "Save Template")
    .Set("leftButtonCaption", "Cancle")
    .Set("rightButtonCaption", "Done")
    .Set("x1",      0)
    .Set("y1",      0)
    .Set("width",   "100%")
    .Set("height",  "10%"));

  // Setting event handler for handling navigation bar click
  this->m_navigationBar->SetEventHandler("clicknavigationbar", this, &CSaveView::OnClickedNavigationBar);

  // Add navigation bar in view
  this->AddChild(this->m_navigationBar);

  // Create text field
  this->m_textField = CreateTextField(CAttributes()
    .Set("name",    "Text_Name")
    .Set("x1",      "5%")
    .Set("y1",      "40%")
    .Set("width",   "90%")
    .Set("height",  "20%")
    .Set("backgroundColour", CColour(COLOUR_GREY))
    );

  // Add text field in view
  this->AddChild(this->m_textField);
}


void CSaveView::Refresh()
{
}


void CSaveView::ShowView(bool show)
{
  this->SetAttribute("visible", show);
  switch(CurrentCommand::GetInstance()->GetNewOrEditCommand())
  {
  case ICommand::NEWTEMPLATE_CMD:
    {
      SetTitleView("Save Template"); 
    }
    break;
  case ICommand::NEWCHECKLIST_CMD:
    {
      SetTitleView("Save Checklist"); 
    }
    break;
  default: break;
  }
}

bool UI::CSaveView::OnClickedNavigationBar( CNavigationBar *naviBar, int button )
{
  switch(button)
  {
  case S3E_NUI_NAVIGATION_BAR_BUTTON_LEFT:
    {
      switch(CurrentCommand::GetInstance()->GetNewOrEditCommand())
      {
      case ICommand::NEWTEMPLATE_CMD:
        {
          this->m_parrentWnd->OnShowView(this, IViewCtrl::CREATETEMPLATEVIEW_T); 
        }
        break;
      case ICommand::NEWCHECKLIST_CMD:
        {
          this->m_parrentWnd->OnShowView(this, IViewCtrl::CREATECHECKLISTVIEW_T); 
        }
        break;
      default: break;
      }
    }
    break;
  case S3E_NUI_NAVIGATION_BAR_BUTTON_RIGHT:
    {
      switch(CurrentCommand::GetInstance()->GetNewOrEditCommand())
      {
      case ICommand::NEWTEMPLATE_CMD:
        {
          ControlCommand::GetInstance()->OnSave(ControlCommand::TEMPLATE_CTRL);
          this->m_parrentWnd->OnShowView(this, IViewCtrl::TEMPLATEVIEW_T); 
        }
        break;
      case ICommand::NEWCHECKLIST_CMD:
        {
          ControlCommand::GetInstance()->OnSave(ControlCommand::CHECKLIST_CTRL);
          this->m_parrentWnd->OnShowView(this, IViewCtrl::CHECKLISTVIEW_T); 
        }
        break;
      default: break;
      }
      SetNameLabel("");
    }
    break;
  default: break;
  }
  return true;
}

void UI::CSaveView::SetTitleView( const std::string& text )
{
  this->m_navigationBar->SetAttribute("title", text.c_str());
}

void UI::CSaveView::SetNameLabel( const std::string& text )
{
  this->m_textField->SetAttribute("text", text.c_str());
}

std::string UI::CSaveView::GetNameLabel()
{
  CString textValue;
  this->m_textField->GetAttribute("text", textValue);
  
  return textValue.Get();
}

bool UI::CSaveView::OnClickedView( CView* view )
{
  return this->m_parrentWnd->HideKeyboard();
}
